
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Binh
 */
public class SunnetCanvas extends Canvas implements MessageListener {

    private Timer timer1;
    private TimerTask timerTask;
    public SunnetGameCanvas gameCanvas;
    public boolean isMenu = true;
    public SunnetMIDlet midlet;
    int curMenu = 0, langId = 1, minMenu = 0;
    int xL = 0, yL = 0;
    //public String[] menu,  menuViet = new String[]{"VÁN MỚI", "ÂM THANH", "ENGLISH", "TÙY CHỌN", "HƯỚNG DẪN", "ĐIỂM CAO", "QUAY LẠI", "THOÁT"};
    public String[] menu,  menuViet = new String[]{"VÁN MỚI", "TIẾP TỤC", "HƯỚNG DẪN", "THOÁT"};
    //public String[] menuEnglish = new String[]{"NEW GAME", "SOUND ON/OFF", "TIẾNG VIỆT", "SETTING", "HELP", "HIGH SCORE", "BACK", "EXIT"};
    public String[] menuEnglish = new String[]{"NEW GAME", "CONTINUES", "HELP", "EXIT"};
    Font fontMenu = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
    Font fontTextMenu = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
     String msgReceived = "no Message";
     MessageConnection serverConn;
     byte[] text;
    public static int DELAY_DEFAULT = 100;
    private int color = 0;
    Image bkImage = null;
    Image mn1 = null;
    Image mn2 = null;
    Image mn3 = null;
    Image mn4 = null;
    public SunnetCanvas(SunnetMIDlet midlet) {
        this.setFullScreenMode(true);
        this.midlet = midlet;
        timer1 = new Timer();

        timerTask = new TimerTask() {

            public void run() {
                if (isShown()) {
                    if (true) {
                        if (xL < 10) {
                            xL++;
                        } else {
                            xL = 0;
                        }

                        if (yL < 10) {
                            yL++;
                        } else {
                            yL = 0;
                        }

                        repaint(0, 0, getWidth(), getHeight());
                    }
                }
            }
        };
        timer1.schedule(timerTask, 0, 100);
    }
    public void setGameCanvas(SunnetGameCanvas gameCanvas){
    }
    protected void paint(Graphics g) {
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());
        //Draw background
        if(bkImage == null){
            try {
                bkImage = Image.createImage("/menu.png");
                mn1 = Image.createImage("/mn1.png");
                mn2 = Image.createImage("/mn2.png");
                mn3 = Image.createImage("/mn3.png");
                mn4 = Image.createImage("/mn4.png");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        int width = getWidth();
        int height = getHeight();
        if(width > 240) {
            width = 240;
        }
        if(height > 300){
            height = 320;
        }
        //g.drawRegion(bkImage, (240-width)/2, (320-height)/2, width, height,0, Math.abs(getWidth()-240)/2, Math.abs(getHeight()-320)/2,  0);
        g.drawRegion(bkImage, (240-width)/2, (320-height)/2, width, height,0, Math.abs(width - 240)/2, Math.abs(height - 320)/2,  0);
        

        if (isMenu) {
            
            if(curMenu == 0)
                g.drawImage(mn1, 57, 143, 0);
            if(curMenu == 1)
                g.drawImage(mn2, 67, 175, 0);
            if(curMenu == 2)
                g.drawImage(mn3, 62, 197, 0);
            if(curMenu == 3)
                g.drawImage(mn4, 88, 232, 0);
             if (langId == 0) {
                menu = menuEnglish;
            } else {
                menu = menuViet;
            }
            //drawMenu(g, getWidth() / 2, (getHeight() - fontMenu.getHeight() * 5) / 2);
        } else {
            g.setColor(255, 255, 255);
            g.drawString(msgReceived, getWidth() / 29, getHeight() / 2, 0);
        }
    }
    public static int VERTICAL = 1;
    public static int HORIZONTAL = 0;

    public void drawMenu(Graphics g, int x, int y) {
        if (langId == 0) {
            menu = menuEnglish;
        } else {
            menu = menuViet;
        }
        g.setFont(fontTextMenu);
        for (int i = minMenu; i < minMenu + 5; i++) {
            if (curMenu == i) {
               color  = 10;
            } else {
               color = 1;
            }
            text = new byte[menu[i].length()];
            Designer.toBytesIndex(menu[i], text);
              Designer.drawCenterString(g,text, 0, menu[i].length(), Designer.FONT_TEXT, color, getWidth()/2, y-6 + (i - minMenu) * 16);
           // Designer.drawString(g,text, 0, menu[i].length(), Designer.FONT_TEXT, color, getWidth()/2-(menu[i].length()-1)*4, y-6 + (i - minMenu) * 16);
        }
        Runtime.getRuntime().gc();
    }

    public static void gradientBox(Graphics g, int color1, int color2, int left, int top, int width, int height, int orientation) {
        int max = orientation == VERTICAL ? height : width;

        for (int i = 0; i < max; i++) {
            int color = midColor(color1, color2, max * (max - 1 - i) / (max - 1), max);

            g.setColor(color);

            if (orientation == VERTICAL) {
                g.drawLine(left, top + i, left + width - 1, top + i);
            } else {
                g.drawLine(left + i, top, left + i, top + height - 1);
            }
        }
    }

    static int midColor(int color1, int color2, int prop, int max) {
        int red =
                (((color1 >> 16) & 0xff) * prop +
                ((color2 >> 16) & 0xff) * (max - prop)) / max;

        int green =
                (((color1 >> 8) & 0xff) * prop +
                ((color2 >> 8) & 0xff) * (max - prop)) / max;

        int blue =
                (((color1 >> 0) & 0xff) * prop +
                ((color2 >> 0) & 0xff) * (max - prop)) / max;

        int color = red << 16 | green << 8 | blue;

        return color;
    }

    private int makeColorAsInt(int alpha, int red, int green, int blue) {
        return alpha << 24 | red << 16 | green << 8 | blue;
    }

    protected void keyPressed(int keyCode) {

        System.out.println(keyCode);

        int instantkeyCode = KeyCodeAdapter.getInstance().adoptKeyCode(keyCode);

        System.out.println(instantkeyCode);

        System.out.println(KeyCodeAdapter.SOFT_KEY_LEFT);


        switch (instantkeyCode) {
            case KeyCodeAdapter.LEFT_KEY:
                //leftkey();

                repaint();
                return;
            case KeyCodeAdapter.RIGHT_KEY:
                // rightkey();
                repaint();

                return;
            case KeyCodeAdapter.DOWN_KEY:
                downKey();
                repaint();

                return;

            case KeyCodeAdapter.UP_KEY:
                upKey();
                repaint();

                return;
            case KeyCodeAdapter.CENTER_KEY:
                fireKey();
                repaint();

                return;

            case KeyCodeAdapter.SOFT_KEY_MIDDLE_INTERNET:
                fireKey();
                repaint();

                return;

            case KeyCodeAdapter.SOFT_KEY_LEFT:
                menuLeftKey();
                repaint();

                return;

            case KeyCodeAdapter.SOFT_KEY_RIGHT:
                menuRightKey();
                repaint();

                return;

        }
    }

    public void upKey() {

        if (curMenu > 0) {
            if (minMenu == curMenu) {
                minMenu--;
            }
            curMenu--;
        } else {
            curMenu = menu.length - 1;
            minMenu = menu.length - 5;
        }

    }
    public void downKey() {

        if (curMenu < (menu.length - 1)) {

            if (curMenu == minMenu + 4) {
                minMenu++;
            }
            curMenu++;

        } else {
            curMenu = 0;
            minMenu = 0;
        }
    }

    public void fireKey() {
        if (isMenu) {

            switch (curMenu) {
                case 0:
                    SunnetMIDlet.continues = false;
                    midlet.showGame();
                    return;
                case 1:

                    return;
                case 2:
                    if (langId == 1) {
                        langId = 0;
                        menu = menuEnglish;
                    } else {
                        langId = 1;
                        menu = menuViet;
                    }

                    return;

            }
        }
    }

    public void menuLeftKey() {
        isMenu = !isMenu;
    }

    public void menuRightKey() {
        
       
    }

    public void notifyIncomingMessage(MessageConnection conn) {
        Message msg = null;
        //  Try reading (maybe block for) a message
        try {
            msg = conn.receive();
        } catch (Exception e) {
            // Handle reading errors
            msgReceived = e.toString();
            System.out.println("processMessage.receive " + e);
        }
      

        // Process the received message
        if (msg instanceof TextMessage) {
            TextMessage tmsg = (TextMessage) msg;
            msgReceived = tmsg.getPayloadText();
        } else {
           

            if (msg instanceof BinaryMessage) {
                BinaryMessage bmsg = (BinaryMessage) msg;
                byte[] data = bmsg.getPayloadData();
                //  Handle the binary message...
                msgReceived = data.toString();

            }
        }
    }
}
